package com.wc.算法基础课.B第二讲数据结构.线段树.小朋友排队;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/24 9:43
 * @description https://www.acwing.com/problem/content/1217/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static int[] a = new int[N];
    // 求最高
    static int[] h = new int[N << 2];
    // 求最矮
    static int[] low = new int[N << 2];
    // 统计左边比自己高的，右边比自己矮的个数
    static int[] num = new int[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
        build(1, n, 1);
        for (int i = 1; i <= n; i++) {
            updateLeft(1, n, 1, 1, i - 1, i);
            updateRight(1, n, 1, i + 1, n, i);
        }
        long res = 0;
        for (int i = 1; i <= n; i++) {
            res += (long) (num[i] + 1) * num[i] / 2;
        }
        out.println(res);
        out.flush();
    }

    static void updateLeft(int l, int r, int t, int ql, int qr, int u) {
        if (ql > qr) return;
        if (l == r) {
            if (a[u] < h[t]) {
                num[u]++;
            }
            return;
        }
        if (l == ql && r == qr) {
            if (low[t] > a[u]) {
                num[u] += r - l + 1;
                return;
            } else if (h[t] <= a[u]) {
                return;
            }
        }
        int mid = l + r >> 1;
        if (mid >= qr) updateLeft(l, mid, t << 1, ql, qr, u);
        else if (mid + 1 <= ql) updateLeft(mid + 1, r, t << 1 | 1, ql, qr, u);
        else {
            updateLeft(l, mid, t << 1, ql, mid, u);
            updateLeft(mid + 1, r, t << 1 | 1, mid + 1, qr, u);
        }
    }

    static void updateRight(int l, int r, int t, int ql, int qr, int u) {
        if (ql > qr) return;
        if (l == r) {
            if (a[u] > h[t]) {
                num[u]++;
            }
            return;
        }
        if (l == ql && r == qr) {
            if (h[t] < a[u]) {
                num[u] += r - l + 1;
                return;
            } else if (low[t] >= a[u]) {
                return;
            }
        }
        int mid = l + r >> 1;
        if (mid >= qr) updateRight(l, mid, t << 1, ql, qr, u);
        else if (mid + 1 <= ql) updateRight(mid + 1, r, t << 1 | 1, ql, qr, u);
        else {
            updateRight(l, mid, t << 1, ql, mid, u);
            updateRight(mid + 1, r, t << 1 | 1, mid + 1, qr, u);
        }
    }

    static void build(int l, int r, int t) {
        if (l == r) {
            h[t] = low[t] = a[l];
            return;
        }
        int mid = l + r >> 1;
        build(l, mid, t << 1);
        build(mid + 1, r, t << 1 | 1);
        pushUp(t);
    }

    static void pushUp(int t) {
        h[t] = Math.max(h[t << 1], h[t << 1 | 1]);
        low[t] = Math.min(low[t << 1], low[t << 1 | 1]);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
